using Occupie.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Occupie.Enums;
using System.IO;
using Occupie.ViewModels;

namespace Occupie.Managers
{
    public class EmployerManager : BaseManager
    {       
        public Employer AddEmployer(int userId, RegisterModelEmployer model)
        {
            try
            {
                Employer employer = new Employer();
                employer.UserId = userId;
                employer.LogoPath = "/Images/company-avatar.png";
                employer.Name = model.Name;
                employer.AdminEmail = model.AdminEmail;
                employer.EIK = model.EIK;
                employer.Email = model.Email;
                employer.Website = model.Website;
                employer.Address = model.Address;

                db.Employers.Add(employer);

                db.SaveChanges();
                return employer;
            }
            catch (Exception)
            {
                return null;
            }            
        }

        public Employer SaveEmployer(Employer employer)
        {
            try
            {
                var dbEntry = db.Employers.Find(employer.EmployerProfileId);

                if (dbEntry != null)
                {
                    dbEntry.Name = employer.Name;
                    dbEntry.Address = employer.Address;

                    dbEntry.Website = employer.Website;
                    dbEntry.LinkedIn = employer.LinkedIn;
                    dbEntry.Email = employer.Email;
                    dbEntry.Phone = employer.Phone;
                    dbEntry.YearFounded = employer.YearFounded;
                    dbEntry.Description = employer.Description;
                    dbEntry.NumberOfEmployees = employer.NumberOfEmployees;

                    dbEntry.DepartmentsString = employer.DepartmentsString;

                    //TODO Add offers
                }

                db.SaveChanges();
                return dbEntry;
            }
            catch (Exception)
            {
                return null;
            }        
        }

        public Employer GetEmployerByUserId(int id)
        {
            Employer employer = db.Employers.FirstOrDefault(x => x.UserId == id);

            return employer;
        }

        public int GetProfileIdByUserId(int id)
        {
            Employer employer = db.Employers.FirstOrDefault(x => x.UserId == id);

            return employer.EmployerProfileId;
        }

        public bool EmployerHasOffer(int offerId, int userId)
        {
            int profileId = GetProfileIdByUserId(userId);
            var employer = db.Employers.FirstOrDefault(x => x.EmployerProfileId == profileId);
            var offer = db.Offers.FirstOrDefault(x=>x.OfferId == offerId);

            if (employer.Offers.Contains(offer))
            {
                return true;
            }

            return false;
        }

        public List<Employer> GetActiveEmployers()
        {
            return db.Employers.Where(x => x.IsActive).ToList();
        }

        public List<EmployerAllViewModel> GetEmployers()
        {
            var employers = new List<EmployerAllViewModel>();
            var activeEmployers = this.GetActiveEmployers();

            foreach (var empl in activeEmployers)
            {
                employers.Add(new EmployerAllViewModel
                {
                    Id = empl.UserId,
                    Name = empl.Name,
                    Picture = empl.LogoPath
                });
            }

            return employers;
        }
    }
}